import { EnumBetResult, EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { RecmInfo, RoundInfo } from "../ExecutePlan";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { ICopy } from "../ICopy";

export interface ILevelJumMoneyStrategyOption {
    baseMoney: number;
    reverse: boolean;
}

export class LevelJumpOptionItem implements ICopy {
    copyValue(valueObj: any): void {
        var obj = valueObj as LevelJumpOptionItem;
        if (obj) {
            this.baseMoney = obj.baseMoney;
            this.prevLimit = obj.prevLimit;
            this.prevLevel = obj.prevLevel;
            this.nextLevel = obj.nextLevel;
            this.nextLimit = obj.nextLimit;
        }
    }
    /**
     * 基础金额
     */
    baseMoney: string | undefined = undefined;
    /**
     * 跳转上一级条件
     */
    prevLimit: string | undefined = undefined;
    /**
     * 跳转位置
     */
    prevLevel: string | undefined = undefined;
    /**
     * 跳转下一级条件
     */
    nextLimit: string | undefined = undefined;
    /**
     * 跳转下一级位置
     */
    nextLevel: string | undefined = undefined;

    checkInfo(ruleCount: number, idx: number): [boolean, string] {
        //return (this.baseMoney && this.prevLevel && this.prevLimit && this.nextLevel && this.nextLimit) ? true : false;
        let prevLevel = parseInt(this.prevLevel ?? '');
        let nextLevel = parseInt(this.nextLevel ?? '');
        let baseMoney = parseInt(this.baseMoney ?? '');
        let msg: string[] = [];
        if (isNaN(baseMoney) || baseMoney < 0) {
            msg.push(`规则${idx + 2}注码无效`);
        }
        //it.prevLevel < 0 || it.prevLevel > ruleCount || it.nextLevel < 0 || it.nextLevel > ruleCount
        if (isNaN(prevLevel) || prevLevel < 0 || prevLevel > (ruleCount + 3)
            || isNaN(nextLevel) || nextLevel < 0 || nextLevel > (ruleCount + 3)) {
            msg.push(`规则${idx + 2}跳转规则无效`);
        }
        return [msg.length == 0, msg.join(';') + "，点击规则检查修改"];
    }
}

export class LevelJumMoneyStrategy implements IMoneyStrategy {
    option: ILevelJumMoneyStrategyOption;
    baseMoney: number = 100;
    _level: number = 0;
    _betIndex: number = 0;
    _reverse: boolean = false;
    /**
     * fasle:输了第一层注码调整，true赢了注码调整
     */
    _direction: boolean = false;
    public get direction() {
        return this._direction;
    }
    public set direction(value: boolean) {
        this._direction = value;
    }
    protected _rules: LevelJumpOptionItem[] = [];
    public get rule(): LevelJumpOptionItem[] {
        return this._rules.slice().reverse();
    }
    public set rule(value: LevelJumpOptionItem[]) {
        this._rules = value;
    }
    protected _minArray: number[] = [];
    protected _minString: string = '';
    public get minString(): string {
        //return this._minArray.join(',');
        return this._minString;
    }
    public set minString(value: string) {
        this._minString = value;
        let array: number[] = [];
        if (this._minString) {
            array = this._minString.split(',').map(it => parseInt(it)).filter(it => !isNaN(it));
        }

        this._minArray = array;
    }
    constructor(name: string, option?: ILevelJumMoneyStrategyOption | null) {
        this.strategyName = name;
        if (option) {
            this.option = option;
        }
        else {
            this.option = {
                baseMoney: 100,
                reverse: false,
            };
        }
        this._rules = [];
        this._minArray = [100, 300, 700, 1500, 3100];
        this._minString = "100,300,700,1500,3100";
        this._reverse = this.option.reverse;
        this.baseMoney = this.option.baseMoney;
        this.resetStrategy();
    }
    showMoney(): [EnumBjlResult, number] {
        throw new Error("Method not implemented.");
    }
    resetRecm(): void {
    }
    resetStrategy(): void {
        this._level = 0;
        this._betIndex = 0;
    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let recmV = roundInfo.userRecm;
        let recmMoney = 0

        // if (recmV != EnumBjlResult.U) {
        //     if (recmInfo.summaryInfo.prevResult == EnumBetResult.Success) {
        //         if (this._reverse) {
        //             recmV = recmV == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.X;
        //         }

        //     }
        //     else if (recmInfo.summaryInfo.prevResult == EnumBetResult.Fail) {
        //         if (!this._reverse) {
        //             recmV = recmV == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.X;
        //         }
        //     }
        // }

        if (this._level < 2) {

            if (recmInfo.summaryInfo.prevResult == EnumBetResult.Success) {
                if (this._direction) {
                    this._betIndex++;
                    if (this._betIndex >= this._minArray.length) {
                        this._level = 2;
                        this._betIndex = 0;
                        recmInfo.roundSummaryInfo.resetSummaryInfo();
                    }
                    else {
                        recmMoney = this._minArray[this._betIndex];
                        return [recmV, recmMoney];
                    }
                }
                else {
                    this._betIndex = 0;
                    recmMoney = this._minArray[this._betIndex];
                    return [recmV, recmMoney];
                }

            }
            else if (recmInfo.summaryInfo.prevResult == EnumBetResult.Fail) {
                if (this._direction) {
                    this._betIndex = 0;
                    recmMoney = this._minArray[this._betIndex];
                    return [recmV, recmMoney];
                }
                else {
                    this._betIndex++;
                    if (this._betIndex >= this._minArray.length) {
                        this._level = 2;
                        this._betIndex = 0;
                        recmInfo.roundSummaryInfo.resetSummaryInfo();
                    }
                    else {
                        recmMoney = this._minArray[this._betIndex];
                        return [recmV, recmMoney];
                    }

                }

            }
            else {
                recmMoney = this._minArray[this._betIndex];
                return [recmV, recmMoney];
            }

        }
        let win = recmInfo.roundSummaryInfo.winCount - recmInfo.roundSummaryInfo.failCount;
        let rule = this._rules[this._level - 2];
        if (!rule) {
            this._level = 0;
            this._betIndex = -1;
            return this.toMoney(roundInfo, recmInfo, userRecm, baseMoney);
        }
        if (recmInfo.summaryInfo.prevResult == EnumBetResult.Success) {
            if (win >= parseInt(rule.prevLimit ?? '0')) {
                let nextLevel = this._rules[parseInt(rule.prevLevel ?? '1') - 2];
                if (nextLevel) {
                    this._level = parseInt(rule.prevLevel ?? '1');
                    recmMoney = parseInt(nextLevel.baseMoney ?? baseMoney.toString());
                    recmInfo.roundSummaryInfo.resetSummaryInfo();
                }
                else if ((parseInt(rule.prevLevel ?? '1') - 2) < 2) {
                    this._level = 0;
                    this._betIndex = 0;
                    recmMoney = this._minArray[this._betIndex];
                }
                else {
                    recmMoney = parseInt(rule.baseMoney ?? baseMoney.toString());
                }
            }
            else {
                recmMoney = parseInt(rule.baseMoney ?? baseMoney.toString());
            }
        }
        else if (recmInfo.summaryInfo.prevResult == EnumBetResult.Fail) {
            if ((-win) >= parseInt(rule.nextLimit ?? '0')) {
                let nextLevel = this._rules[parseInt(rule.nextLevel ?? '1') - 2];
                if (nextLevel) {
                    this._level = parseInt(rule.nextLevel ?? '1');
                    recmMoney = parseInt(nextLevel.baseMoney ?? baseMoney.toString());
                    recmInfo.roundSummaryInfo.resetSummaryInfo();
                }
                else if ((parseInt(rule.nextLevel ?? '1') - 2) < 2) {
                    this._level = 0;
                    this._betIndex = 0;
                    recmMoney = this._minArray[this._betIndex];
                }
                else {
                    recmMoney = parseInt(rule.baseMoney ?? baseMoney.toString());
                }
            }
            else {
                recmMoney = parseInt(rule.baseMoney ?? baseMoney.toString());
            }
        }
        else {
            recmMoney = parseInt(rule.baseMoney ?? baseMoney.toString());
        }

        if (recmMoney < this.baseMoney) {
            recmMoney = this.baseMoney;
        }


        return [recmV, recmMoney];
    }

    strategyName: string;
    debugCompoenent: string = "";
    copyValue(valueObj: any): void {
        let obj = valueObj as LevelJumMoneyStrategy;
        if (obj) {
            this.option = obj.option;
            this.baseMoney = obj.baseMoney;
            this._rules = obj._rules;
            this._minArray = obj._minArray;
            this._minString = obj._minString;
            this._level = obj._level;
            this._betIndex = obj._betIndex;
            this._direction = obj._direction;
        }

    }

    getRuleCount(): number {
        return this._rules.length;
    }
    getRule(idx: number): LevelJumpOptionItem {
        return this._rules[idx];
    }
    addRule(rule: LevelJumpOptionItem): void {
        this._rules.push(rule);
    }

    editRule(idx: number, rule: LevelJumpOptionItem): void {
        if (rule) {
            this._rules.splice(idx, 1, rule);
        }
        else {
            this._rules.splice(idx, 1);
        }
    }
    removeRule(idx: number) {
        this._rules.splice(idx, 1);
    }
    checkFule(): [boolean, string] {
        let ruleCount = this._rules.length;
        let msg: string[] = [];
        this._rules.forEach((it, idx) => {
            let prevLevel = parseInt(it.prevLevel ?? '');
            let nextLevel = parseInt(it.nextLevel ?? '');
            //it.prevLevel < 0 || it.prevLevel > ruleCount || it.nextLevel < 0 || it.nextLevel > ruleCount
            if (isNaN(prevLevel) || prevLevel < 0 || prevLevel > ruleCount
                || isNaN(nextLevel) || nextLevel < 0 || nextLevel > ruleCount) {
                msg.push(`规则${idx + 2}跳转规则无效，点击规则检查修改`);
            }
        });
        return [msg.length == 0, msg.join(";")];
    }
}